<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\form\util\control\dctrl\items;

use yii\helpers\ArrayHelper;

/**
 * 多选框控件
 * @author cleverstone
 * @since ym2.0
 */
class Checkbox extends Dcontrol
{
    /**
     * @var array
     */
    private $_options = [
        'control' => 'checkbox',
        'label' => '',
        'default' => [],
        'options' => [],
        'required' => false,
        'comment' => '',
        'help' => '',
        'thClass' => '',
        'thStyle' => '',
        'style' => '',
        'class' => '',
        'attribute' => [],
        'minWidth' => '',
        'disabled' => [],
    ];

    /**
     * 禁用值
     * @param array $items
     * ```php
     * $this->disabled(['value1', 'value2']);
     * ```
     * @return $this
     */
    public function disabled(array $items)
    {
        $this->_options['disabled'] = $items;
        return $this;
    }

    /**
     * 单元格最小宽度
     * @param integer|string $minWidth
     * @return $this
     */
    public function minWidth($minWidth)
    {
        if (is_numeric($minWidth)) {
            $minWidth .= 'px';
        }

        $this->_options['minWidth'] = $minWidth;
        return $this;
    }

    /**
     * 标签风格
     * @return $this
     */
    public function tag()
    {
        $this->_options['attribute'] = ArrayHelper::merge(['lay-skin' => 'tag'], $this->_options['attribute']);
        return $this;
    }

    /**
     * 选项
     * @param array $options
     * @return $this
     */
    public function options(array $options)
    {
        $this->_options['options'] = $options;
        return $this;
    }

    /**
     * 控件标签
     * @param string $label
     * @return $this
     */
    public function label($label)
    {
        $this->_options['label'] = (string)$label;
        return $this;
    }

    /**
     * 默认值
     * @param array $default
     * ```php
     * $this->defaultValue(['value1', 'value2'])
     *
     * ```
     * @return $this
     */
    public function defaultValue(array $default)
    {
        $valueArray = array_fill(0, count($default), 1);
        $this->_options['default'] = array_combine($default, $valueArray);

        return $this;
    }

    /**
     * 是否必填
     * @param bool $required
     * @return $this
     */
    public function required($required = true)
    {
        $this->_options['required'] = (boolean)$required;
        return $this;
    }

    /**
     * 字段注释
     * @param string $comment
     * @return $this
     */
    public function comment($comment)
    {
        $this->_options['comment'] = (string)$comment;
        return $this;
    }

    /**
     * 帮助文字
     * @param string $help
     * @return $this
     */
    public function help($help)
    {
        $this->_options['help'] = (string)$help;
        return $this;
    }

    /**
     * th CSS样式
     * @param array|string $style
     * @return $this
     */
    public function thStyle($style)
    {
        $this->formatStyle($style);
        $this->_options['thStyle'] = $style;

        return $this;
    }

    /**
     * th UI
     * @param array|string $class
     * @return $this
     */
    public function thUiClass($class)
    {
        $this->formatUiClass($class);
        $this->_options['thClass'] = $class;

        return $this;
    }

    /**
     * td CSS样式
     * @param array|string $style
     * @return $this
     */
    public function style($style)
    {
        $this->formatStyle($style);
        $this->_options['style'] = $style;

        return $this;
    }

    /**
     * td UI
     * @param array|string $class
     * @return $this
     */
    public function uiClass($class)
    {
        $this->formatUiClass($class);
        $this->_options['class'] = $class;

        return $this;
    }

    /**
     * HTML属性
     * @param array $attribute
     * @return $this
     */
    public function attribute(array $attribute)
    {
        $this->_options['attribute'] = ArrayHelper::merge($this->_options['attribute'], $attribute);
        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        $options = $this->_options;
        $this->formatHtmlAttribute($options['attribute']);

        if (empty($options['default'])) {
            $valueArray = array_fill(0, count($options['options']), 0);
            $options['default'] = array_combine(array_keys($options['options']), $valueArray);
        }

        return $options;
    }
}